package com.rjwh.dingdong.client.activity.homework;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.homework.fragment.HomeWorkFragment;
import com.rjwh.dingdong.client.activity.message.MessageListActivity;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.widget.PagerSlidingTabStrip;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 王小珂
 * @ClassName HomeWorkActivity
 * @Description
 * @date 2016-12-30
 * @history 2016-12-30 author: description:
 */
public class HomeWorkActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.more_homework_pagelidingtab)
    PagerSlidingTabStrip moreHomeworkPagelidingtab;
    @BindView(R.id.more_homework_viewpager)
    ViewPager moreHomeworkViewpager;

    private ArrayList<BaseFragment> listFragment = new ArrayList<>();
    private MenuItem menuItemView;
    private int currentItem;
    private int currentAgeID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_homework_layout);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initToolbarHelper();
        listFragment.add(HomeWorkFragment.newInstance("math"));
        listFragment.add(HomeWorkFragment.newInstance("sinology"));
        listFragment.add(HomeWorkFragment.newInstance("classic"));
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        moreHomeworkViewpager.setAdapter(adapter);
        moreHomeworkPagelidingtab.setViewPager(moreHomeworkViewpager);
        initTabsValue();
    }

    /**
     * init the toolbar
     */
    protected void initToolbarHelper() {
        toolbar.setTitle("");
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(R.string.homework_share);
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.menu_drawbook_catalog, menu);
        return true;
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menuItemView = menu.findItem(R.id.menu_catalog_all);
        ((TextView) menuItemView.getActionView()).setText("3-4岁");
        menuItemView.getActionView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOptionsItemSelected(menuItemView);
            }
        });
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_catalog_all:
                showAgeSelectDialog();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 年龄选择框
     */
    private void showAgeSelectDialog() {
        final String[] names = {"3-4岁", "4-5岁", "5-6岁"};
        new AlertDialog.Builder(this)
                .setItems(names, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (currentAgeID != which) {
                            currentAgeID = which;
                            updateAge(names[which], which);
                        }
                        dialog.dismiss();
                    }
                })
                .show();
    }

    private void updateAge(String string, int which) {
        ((TextView) menuItemView.getActionView()).setText(string);
        currentItem = moreHomeworkViewpager.getCurrentItem();
        HomeWorkFragment currentFm = (HomeWorkFragment) listFragment.get(currentItem);
        currentFm.setAgeCode(which);
        currentFm.doRefresh();
    }

    /**
     * mPagerSlidingTabStrip默认值配置
     */
    private void initTabsValue() {

        // 底部游标颜色@color/blue_bg
        moreHomeworkPagelidingtab.setIndicatorColor(getResources().getColor(R.color.blue_button_nor));
        moreHomeworkPagelidingtab.setTabPaddingLeftRight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // tab的分割线颜色
        moreHomeworkPagelidingtab.setDividerColor(Color.TRANSPARENT);
        // tab背景
        moreHomeworkPagelidingtab.setBackgroundColor(getResources().getColor(R.color.white));
        // tab底线高度
        moreHomeworkPagelidingtab.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 游标高度
        moreHomeworkPagelidingtab.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 选中的文字颜色
        moreHomeworkPagelidingtab.setSelectedTextColor(getResources().getColor(R.color.blue_button_nor));
        // 正常文字颜色
        moreHomeworkPagelidingtab.setTextColor(Color.BLACK);
        moreHomeworkPagelidingtab.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        moreHomeworkPagelidingtab.setShouldExpand(true);

    }

    /* ***************FragmentPagerAdapter***************** */
    public class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {"逻辑数学", "国学绘本", "分级阅读"};

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            // TODO Auto-generated method stub
            return super.isViewFromObject(view, object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            // super.destroyItem(container, position, object);
        }
    }

    @OnClick({R.id.more_homework_rank_btn, R.id.more_homework_fans_btn, R.id.more_homework_mine_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more_homework_mine_btn:
                //我的作业
                MessageListActivity.startActivity(this, getString(R.string.message_type_homework));
                break;
            case R.id.more_homework_rank_btn:
                //排行榜
                HomeWorkRankActivity.startActivity(this,"");
                break;
            case R.id.more_homework_fans_btn:
                //我的粉丝
                HomeWorkFansActivity.startActivity(this);
                break;
        }
    }
}
